package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.print.attribute.standard.MediaSize;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {


    @Autowired
    CheckGroupMapper checkGroupMapper;

    //新增
    @Override
    public Result add(CheckGroup checkGroup, Integer[] checkitemIds) {
        if (checkitemIds.length == 0) {
            checkGroupMapper.insert(checkGroup);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } else {
            checkGroupMapper.insert(checkGroup);
            Map map = new HashMap<>();
            map.put("checkgroupId", checkGroup.getId());
            map.put("checkitemIds", checkitemIds);
            //添加中间表数据
            checkGroupMapper.addCheckGroupOrCheckItem(map);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }
    }

    //删除
    @Override
    public Result delete(Integer id) {
        //删除中间表信息
        checkGroupMapper.deleteCheckGroupOrCheckItemById(id);
        //删除主表信息
        checkGroupMapper.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Page<CheckGroup> page = new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List list;
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0) {
            list = checkGroupMapper.selectPage(page,
                    new EntityWrapper<CheckGroup>()
                            .eq("code", queryPageBean.getQueryString())
                            .or()
                            .eq("name", queryPageBean.getQueryString())
            );
        } else {
            list = checkGroupMapper.selectPage(page, null);
        }
        return new PageResult(page.getTotal(), list);
    }

    //修改
    @Override
    public Result update(CheckGroup checkGroup, Integer[] checkitemIds) {
        //通过id删除中间表信息
        checkGroupMapper.deleteCheckGroupOrCheckItemById(checkGroup.getId());
        //先修改主表的信息
        checkGroupMapper.deleteById(checkGroup.getId());
        if (checkitemIds.length == 0) {
            checkGroupMapper.insert(checkGroup);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } else {
            checkGroupMapper.insert(checkGroup);
            Map map = new HashMap<>();
            map.put("checkgroupId", checkGroup.getId());
            map.put("checkitemIds", checkitemIds);
            //添加中间表数据
            checkGroupMapper.addCheckGroupOrCheckItem(map);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }

    }

    //通过id查询
    @Override
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.selectById(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    //通过id查询中间表id数组
    @Override
    public Result findCheckGroupOrCheckItemById(Integer id) {
        List checkGroupOrCheckItemById = checkGroupMapper.findCheckGroupOrCheckItemById(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupOrCheckItemById);
    }

    @Override
    public Result findAll() {
        List<CheckGroup> list = checkGroupMapper.selectList(null);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }



//1465
//656
//123
//wqer
//wqewer
}
